package com.asa.common.net.factory

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import okhttp3.RequestBody
import okhttp3.ResponseBody
import retrofit2.Converter
import retrofit2.Retrofit
import java.lang.reflect.Type

class DecodeConverterFactory(private val gson:Gson):Converter.Factory() {


        companion object {
            @JvmStatic
            fun create(): DecodeConverterFactory {
                return create(Gson())
            }

            fun create(gson: Gson): DecodeConverterFactory {
                return DecodeConverterFactory(gson)
            }
        }

    override fun requestBodyConverter(
        type: Type,
        parameterAnnotations: Array<out Annotation>,
        methodAnnotations: Array<out Annotation>,
        retrofit: Retrofit
    ): Converter<*, RequestBody>? {
        val adapter = gson.getAdapter(TypeToken.get(type))
        return DecodeRequestBodyConverter(gson, adapter)
    }

    override fun responseBodyConverter(
        type: Type,
        annotations: Array<out Annotation>,
        retrofit: Retrofit
    ): Converter<ResponseBody, *>? {
        val adapter = gson.getAdapter(TypeToken.get(type))
        return DecodeResponseBodyConverter(adapter)
    }


}